package com.ruoyi.server.api.imgtype.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.common.core.domain.entity.ImgType;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.server.api.imgtype.service.IImgTypeService;
import com.ruoyi.common.utils.poi.ExcelUtil;

/**
 * 图片分类Controller
 * 
 * @author ruoyi
 * @date 2024-06-20
 */
@RestController
@RequestMapping("/imgtype/imgtype")
public class ImgTypeController extends BaseController
{
    @Autowired
    private IImgTypeService imgTypeService;

    /**
     * 查询图片分类列表
     */
    @PreAuthorize("@ss.hasPermi('imgtype:imgtype:list')")
    @GetMapping("/list")
    public AjaxResult list(ImgType imgType)
    {
        List<ImgType> list = imgTypeService.selectImgTypeList(imgType);
        return success(list);
    }
    /**
     * 查询图片分类列表
     */
    @GetMapping("/parentIdList")
    public AjaxResult imgTypeByIdList(Long id)
    {
        List<ImgType> list = imgTypeService.selectImgTypeByParentIdList(id);
        return success(list);
    }

    /**
     * 导出图片分类列表
     */
    @PreAuthorize("@ss.hasPermi('imgtype:imgtype:export')")
    @Log(title = "图片分类", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, ImgType imgType)
    {
        List<ImgType> list = imgTypeService.selectImgTypeList(imgType);
        ExcelUtil<ImgType> util = new ExcelUtil<ImgType>(ImgType.class);
        util.exportExcel(response, list, "图片分类数据");
    }

    /**
     * 获取图片分类详细信息
     */
    @PreAuthorize("@ss.hasPermi('imgtype:imgtype:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(imgTypeService.selectImgTypeById(id));
    }

    /**
     * 新增图片分类
     */
    @PreAuthorize("@ss.hasPermi('imgtype:imgtype:add')")
    @Log(title = "图片分类", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody ImgType imgType)
    {
        return toAjax(imgTypeService.insertImgType(imgType));
    }

    /**
     * 修改图片分类
     */
    @PreAuthorize("@ss.hasPermi('imgtype:imgtype:edit')")
    @Log(title = "图片分类", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody ImgType imgType)
    {
        return toAjax(imgTypeService.updateImgType(imgType));
    }

    /**
     * 删除图片分类
     */
    @PreAuthorize("@ss.hasPermi('imgtype:imgtype:remove')")
    @Log(title = "图片分类", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(imgTypeService.deleteImgTypeByIds(ids));
    }

    /**
     * @Author zsc
     * @Description 根据子id查询父id
     * @Date 2024/10/25 15:51
     * @Return com.ruoyi.common.core.domain.AjaxResult
     * @param id
     */
    @GetMapping("/getParentIdByChildId")
    public AjaxResult getParentIdByChildId(Long id){
        return success(imgTypeService.getParentIdByChildId(id));
    }
}
